<?php

namespace app\admin\controller;

use app\common\business\User as userBn;
use think\Db;
use think\facade\Request;
use app\common\business\Match as matchBn;
class Match extends Common {

    protected $dao, $fields;

    public function _initialize() {
        parent::_initialize();

        $this->dao = db('match');

        unset($fields);
        unset($res);
        $this->assign('fields', $this->fields);
    }

    public function index() {

        if (request()->isPost()) {

            $model = db('match');
            $status = input('post.status');
            $key = input('post.key');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            if(!empty($key) ){
                $map['home_team|guest_team']=$key;

            }
            $list = $model
                    ->where($map)
                    ->order('match_time asc')
                    ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                    ->toArray();
            $lists = $list['data'];
            foreach ($lists as $k => $v) {
                $lists[$k]['match_time'] = date("Y-m-d H:i", $v['match_time']);
                // 0:未开放，1，答题中，2，关闭 3.显示答案
                if ($v['status'] == 1) {
                    $lists[$k]['status'] = '今日答题';
                } elseif ($v['status'] == 0) {
                    $lists[$k]['status'] = '未显示';
                } elseif ($v['status'] == 2) {
                    $lists[$k]['status'] = '关闭';
                } elseif ($v['status'] == 3) {
                    $lists[$k]['status'] = '显示答案';
                }
            }
            $rsult['data'] = $lists;
            $rsult['count'] = $list['total'];

            $rsult['msg'] = "获取成功";
            $rsult['code'] = 0;
            $rsult['rel'] = 1;
            return $rsult;
        } else {
            return $this->fetch();
        }
    }
    public function guess() {

        if (request()->isPost()) {

            $model = db('guess');
            $mobile=input('mobile');
            $map=array();
            if(empty($mobile) ){

            }else{
                if($mobile==1){
                    $map['mobile']>0;
                }else{
                    $map['mobile']=0;
                }

            }

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');

            $list = $model
                ->order('add_time desc')
                ->alias('g')
                ->join('__USERS__ u','u.user_id=g.user_id','left')
                ->where($map)
                ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                ->toArray();
            $lists = $list['data'];
            foreach ($lists as $k => $v) {
                $lists[$k]['add_time'] = date("Y-m-d H:i", $v['add_time']);
                $userinfo=userBn::getUserInfo(array('user_id'=>$v['user_id']));
                $lists[$k]['username'] ='('.$userinfo['user_id'].')'.($userinfo['nickname']?$userinfo['nickname']:'球宝'.$userinfo['user_id']);
                $lists[$k]['mobile'] =$userinfo['mobile'];
            }
            $rsult['data'] = $lists;
            $rsult['count'] = $list['total'];

            $rsult['msg'] = "获取成功";
            $rsult['code'] = 0;
            $rsult['rel'] = 1;
            return $rsult;
        } else {
            return $this->fetch();
        }
    }
    public function add() {
        if (Request::isAjax()) {
            //构建数组
            $data = Request::except('file');

            $home = db('national_team')->where(array('team_id' => $data['home_id']))->find();
            $data['home_logo'] = $home['team_logo'];
            $data['home_team'] = $home['team_name'];
            $guest = db('national_team')->where(array('team_id' => $data['guest_id']))->find();
            $data['guest_logo'] = $guest['team_logo'];
            $data['guest_team'] = $guest['team_name'];
            $data['match_time'] = strtotime($data['match_time']);
            db('match')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!';
            cache('adList', NULL);
            $result['url'] = url('index');
            return $result;
        } else {
            $adtypeList = db('national_team')->order('team_group asc')->field('team_id,team_id as home_id ,team_id as guest_id,team_name')->select();
            $this->assign('teamlist', json_encode($adtypeList, true));
            $this->assign('title', lang('add'));
            $this->assign('info', 'null');
            $this->assign('selected', 'null');
            $this->assign('selectedguest', 'null');
            return $this->fetch('form');
        }
    }
    public function matchdel(){
        $id = input('post.id');
        $model = db('match');
        $model->where(array('match_id'=>$id))->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function delmatchAll(){
        $map[] = array('match_id', 'IN', input('param.ids/a'));
        $model =  db('match');
        $model->where($map)->delete();
        $result['code'] = 1;
        $result['msg'] = '删除成功！';
        $result['url'] = url('index');
        return $result;
    }
    public function edit() {
        if (Request::isAjax()) {
            $data = Request::except('file');
            $ad_id = input('match_id');
            $adInfo = db('match')->where(array('match_id' => $ad_id))->find();
            if($adInfo['status']==3){
                if($data['status']!=3){
                    $result['code'] = 0;
                    $result['msg'] = '已经显示答案，不可更改状态!';

                    return $result;
                }
            }else{
                if($data['status']==3){
                    $map[]=$data['match_id'];
                    $falg=matchBn::sumAskPoint($map);
                    if($falg){
                        $result['code'] = 1;
                        $result['msg'] = '显示成功！';
                        $result['url'] = url('index');
                    }else{
                        $result['code'] = 0;
                        $result['msg'] = '已显示答案或者更新条件未改变！';
                        $result['url'] = url('index');
                    }

                }

            }

            $home = db('national_team')->where(array('team_id' => $data['home_id']))->find();
            $data['home_logo'] = $home['team_logo'];
            $data['home_team'] = $home['team_name'];
            $guest = db('national_team')->where(array('team_id' => $data['guest_id']))->find();
            $data['guest_logo'] = $guest['team_logo'];
            $data['guest_team'] = $guest['team_name'];
            $data['match_time'] = strtotime($data['match_time']);
            db('match')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('index');
            return $result;
        } else {
            $ad_id = input('match_id');

            $adInfo = db('match')->where(array('match_id' => $ad_id))->find();
            $adInfo['match_time'] = date('Y-m-d H:i', $adInfo['match_time']);
            $adtypeList = db('national_team')->order('team_group asc')->field('team_id,team_id as home_id ,team_id as guest_id,team_name')->select();
            $this->assign('teamlist', json_encode($adtypeList, true));
            $selectedhome = array('home_id' => $adInfo['home_id'], 'team_name' => $adInfo['home_team']);
            $selectedguest = array('guest_id' => $adInfo['guest_id'], 'team_name' => $adInfo['guest_team']);
            $this->assign('selected', json_encode($selectedhome, true));
            $this->assign('selectedguest', json_encode($selectedguest, true));
            $this->assign('info', json_encode($adInfo, true));
            $this->assign('title', lang('edit'));
            return $this->fetch('form');
        }
    }


    public function todayAll() {
        $map[] = array('match_id', 'IN', input('param.ids/a'));
        $model = db('match');
        $matchinfo=  $model->where($map)->where(array('status' => 3))->column('match_id');
//        $map[0][2]=array_diff($map[0][2],$matchinfo);

        if($matchinfo){
//
            $result['msg'] = "编号为".implode(',',$matchinfo).'已经显示答案，不可更改状态！';
            $result['code'] = 0;
            $result['rel'] = 1;
        }else{
            $model = db('match');
            $model->where($map)->update(array('status' => 1));
            $result['code'] = 1;
            $result['msg'] = '批量成功！';
            $result['url'] = url('index');
        }

        return $result;
    }

    public function closeAll() {
        $map[] = array('match_id', 'IN', input('param.ids/a'));

        $model = db('match');
        $matchinfo=  $model->where($map)->where(array('status' => 3))->column('match_id');
        if($matchinfo){
            $result['msg'] = "编号为".implode(',',$matchinfo).'已经显示答案，不可更改状态！';
            $result['code'] = 0;
            $result['rel'] = 1;
        }else{
            $model = db('match');
            $model->where($map)->update(array('status' => 2));
            $result['code'] = 1;
            $result['msg'] = '批量成功！';
            $result['url'] = url('index');
        }
        return $result;
    }

    public function showAll() {
        $map[] = array('match_id', 'IN', input('param.ids/a'));
        $model = db('match_ask');
        $match_id = input('param.ids/a');
        if(input('param.ids/a')){
            $match_id=' match_id in ('.implode(',',$match_id) .') and';
        }else{
            $match_id='';
        }
        $askinfo=$model->where($match_id.' ask_result is null')->column('match_id');
        if($askinfo){
            $askinfo=array_unique($askinfo);
            $result['msg'] = "编号为".implode(',',$askinfo)."有竞彩没有处理答案";
            $result['code'] = 0;
            $result['rel'] = 1;
        }else{
//            $model->where($map)->update(array('status' => 3,'update_time'=>time()));
            $falg=matchBn::sumAskPoint($map[0][2]);
            if($falg){
                $result['code'] = 1;
                $result['msg'] = '显示成功！';
                $result['url'] = url('index');
            }else{
                $result['code'] = 0;
                $result['msg'] = '已显示答案或者更新条件未改变！';
                $result['url'] = url('index');
            }
        }
        return $result;
    }

    public function hiddenAll() {
        $map['id'] = array('in', input('param.ids/a'));
        $model = $this->dao;
        $model->where($map)->update(array('status' => 0));
        $result['code'] = 1;
        $result['msg'] = '隐藏成功！';
        $result['url'] = url('index');
        return $result;
    }

    public function getRegion() {
        $Region = db("region");
        $map['pid'] = input("pid");
        $list = $Region->where($map)->select();
        return $list;
    }

    public function matchask() {

        $match_id = input("match_id");
        if (request()->isPost()) {
            $model = db('match_ask');
            $map['match_id'] = input('match_id');

            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $list = $model
                    ->where($map)
                    ->order('ask_sort desc')
                    ->paginate(array('list_rows' => $pageSize, 'page' => $page))
                    ->toArray();
            $lists = $list['data'];
            foreach ($lists as $k => $v) {
                $askcontent = explode(";", $lists[$k]['ask_content']);
                $content = '';
                foreach ($askcontent as $kk => $vv) {
                    $content .=" <span class='red'>(" . ($kk + 1) . ")</span> " . $vv;
                }
                $lists[$k]['ask_content'] = $content;
                if ($lists[$k]['ask_result'] >= 0) {
                    $lists[$k]['ask_result'] = $askcontent[$lists[$k]['ask_result']];
                }
            }
            $rsult['data'] = $lists;
            $rsult['count'] = $list['total'];

            $rsult['msg'] = "获取成功";
            $rsult['code'] = 0;
            $rsult['rel'] = 1;
            $rsult['match_id'] = $match_id;
            return $rsult;
        } else {
            $this->assign('match_id', $match_id);
            return $this->fetch();
        }
    }
    public function del(){
        $id = input('post.id');
        $model = db('match_ask');
        $model->where(array('ask_id'=>$id))->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function delAll(){
        $map[] = array('ask_id', 'IN', input('param.ids/a'));
        $model =  db('match_ask');
        $model->where($map)->delete();
        $result['code'] = 1;
        $result['msg'] = '删除成功！';
        $result['url'] = url('index');
        return $result;
    }
    public function askadd() {
        $match_id = input("match_id");
        if (Request::isAjax()) {
            //构建数组
            $data = Request::except('file');
            if(!is_numeric($data['ask_result'])){
                unset($data['ask_result']);
            }
            db('match_ask')->insert($data);
            $result['code'] = 1;
            $result['msg'] = '添加成功!'; 
            $result['url'] = url('matchask')."?match_id=".$match_id;
            return $result;
        } else {
            
            $this->assign('title', lang('add'));
            $this->assign('match_id', $match_id); 
            return $this->fetch('askform');
        }
    }

    public function askedit() {
        if (Request::isAjax()) {
            $data = Request::except('file');
            if(!is_numeric($data['ask_result'])){
                unset($data['ask_result']);
            }
            db('match_ask')->update($data);
            $result['code'] = 1;
            $result['msg'] = '修改成功!';
            $result['url'] = url('matchask')."?match_id=".$data['match_id'];
            return $result;
        } else {
            $ad_id = input('ask_id');

            $adInfo = db('match_ask')->where(array('ask_id' => $ad_id))->find();
            $this->assign('info', $adInfo);
            $this->assign('match_id', $adInfo['match_id']);
            $this->assign('title', lang('edit'));
            return $this->fetch('askform');
        }
    }

    public function listorder() {
        $model = db('match_ask');
        $data = input('post.');
        $model->update($data);
        $result = ['msg' => '排序成功！', 'url' => url('matchask'), 'code' => 1];
        return $result;
    }

}
